package com.bojx.cms.web;



import com.bojx.cms.annotation.Permission;
import com.bojx.cms.interceptor.AuthInterceptor;
import com.bojx.cms.model.Article;
import com.bojx.cms.model.Question;
import com.bojx.cms.render.VelocityLayoutRender;
import com.bojx.cms.service.UserService;
import com.jfinal.aop.Clear;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.render.JsonRender;

/**
 * 
 * @author syh
 * user module
 */
@Permission("个人中心管理")
public class UserController extends BaseController {
	
	
	public static UserService userService=new UserService();
	
	@Clear(AuthInterceptor.class)
	public void center(){
		render("center.html");
	}
	
	@Permission("文章管理")
	public void newArticle(){
		render(new VelocityLayoutRender("editor.html"));
	}
	
	/**
	 * public article interface
	 */
	@Permission("新建文章")
	public void publish(){
	   Record result=new Record();
	   try {
		   Article article=new Article();
		   article.set("categroy_id", getParaToLong("categroy_id"));
		   article.set("title", getPara("title"));
		   article.set("content", getPara("content"));
		   article.set("user_name", getCookie("username"));
		   userService.save(article);
		   result.set("code", "00");
		   result.set("msg","提交成功");
		   render(new JsonRender(result).forIE());
	   } catch (Exception e) {
		   LOG.error(e.getMessage(), e);
		   result.set("code", "06");
		   result.set("msg", e.getMessage());
		   render(new JsonRender(result).forIE());
	   }
	}
	
	/**
	 * my article
	 */
	@Permission("我的文章")
	public void  myArticle(){
		render(new VelocityLayoutRender("my_article.html"));
	}
	
	/**
	 * my article data
	 */
	@Clear(AuthInterceptor.class)
	public void myArticleData(){
		try{
			int pageNum = getParaToInt("page", 1);
			int rows = getParaToInt("rows", 30);
			pageNum = pageNum > 0 ? pageNum : 1;
			String username=getCookie("username");
			Page<Record> page = Db.paginate(pageNum, rows,"select id,title,create_time ", "from cms_article where user_name='"+username+"'");
			setAttrs(buildPagination(page.getList(), page.getTotalRow()));
			render(new JsonRender().forIE());
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
		}
	}
	
	/**
	 * delete article
	 */
	@Permission("删除文章")
	public void delArticle(){
		Record result=new Record();
		try{
			long id=getParaToLong("id");
			Db.deleteById("cms_article", id);
			result.set("code", "00");
			result.set("msg", "删除成功");
			
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
			result.set("code", "08");
			result.set("msg", "删除失败");
		}finally{
			render(new JsonRender(result).forIE());
		}
	}
	
	/**
	 * my question
	 */
	@Permission("我的问题")
	public void myQuestion(){
		render(new VelocityLayoutRender("my_question.html"));
	}
	
	@Clear(AuthInterceptor.class)
	public void myQuestionData(){
		try{
			int pageNum = getParaToInt("page", 1);
			int rows = getParaToInt("rows", 30);
			pageNum = pageNum > 0 ? pageNum : 1;
			String username=getCookie("username");
			Page<Record> page = Db.paginate(pageNum, rows,"select id,title,create_time ", "from cms_question where user_name='"+username+"'");
			setAttrs(buildPagination(page.getList(), page.getTotalRow()));
			render(new JsonRender().forIE());
		}catch(Exception e){
			LOG.error(e.getMessage(),e);
		}
	}
	
	@Permission("新建提问页面")
	public void newQuestion(){
		render(new VelocityLayoutRender("new_question.html"));
	}
	
	/**
	 * public article interface
	 */
	@Permission("新建提问")
	public void newQuestionPost(){
	   Record result=new Record();
	   try {
		   Question question=new Question();
		   question.set("title", getPara("title"));
		   question.set("desc", getPara("desc"));
		   question.set("user_name", getCookie("username"));
		   userService.save(question);
		   result.set("code", "00");
		   result.set("msg","提交成功");
		   render(new JsonRender(result).forIE());
	   } catch (Exception e) {
		   LOG.error(e.getMessage(), e);
		   result.set("code", "06");
		   result.set("msg", e.getMessage());
		   render(new JsonRender(result).forIE());
	   }
	}
	
}
